﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 列表的基类。
	/// </summary>
	public abstract class BaseList : System.Web.UI.UserControl
	{
		private LoadedValue<int?> loadedSiteId;
		/// <summary>
		/// 列表的当前站点ID。
		/// </summary>
		public virtual int? SiteId
		{
			get
			{
				if (loadedSiteId.Loader == null)
					loadedSiteId.Loader = () => Sites.CurrentSiteId;
				return loadedSiteId.Value;
			}
			set { }
		}

		private LoadedValue<string> loadedListTitle;
		/// <summary>
		/// 列表的标题。
		/// </summary>
		public virtual string ListTitle
		{
			get
			{
				if (loadedListTitle.Loader == null)
					loadedListTitle.Loader = () => (string)ViewState["ListTitle"];
				return loadedListTitle.Value;
			}
			set
			{
				if (loadedListTitle.Setter == null)
					loadedListTitle.Setter = v => ViewState["ListTitle"] = v;
				loadedListTitle.Value = value;
			}
		}

		private LoadedValue<string> loadedListMoreUrl;
		/// <summary>
		/// 列表的更多Url。
		/// </summary>
		public virtual string ListMoreUrl
		{
			get
			{
				if (loadedListMoreUrl.Loader == null)
					loadedListMoreUrl.Loader = () => (string)ViewState["ListMoreUrl"];
				return loadedListMoreUrl.Value;
			}
			set
			{
				if (loadedListMoreUrl.Setter == null)
					loadedListMoreUrl.Setter = v => ViewState["ListMoreUrl"] = v;
				loadedListMoreUrl.Value = value;
			}
		}

		private LoadedValue<string> loadedListTarget;
		/// <summary>
		/// 列表更多时的目标窗口。
		/// </summary>
		public virtual string ListTarget
		{
			get
			{
				if (loadedListTarget.Loader == null)
					loadedListTarget.Loader = () => (string)ViewState["ListTarget"];
				return loadedListTarget.Value;
			}
			set
			{
				if (loadedListTarget.Setter == null)
					loadedListTarget.Setter = v => ViewState["ListTarget"] = v;
				loadedListTarget.Value = value;
			}
		}




		private LoadedValue<int?> loadedLoadCount;
		/// <summary>
		/// 读取的数量。
		/// </summary>
		public virtual int? LoadCount
		{
			get
			{
				if (loadedLoadCount.Loader == null)
					loadedLoadCount.Loader = () => (int?)ViewState["LoadCount"];
				return loadedLoadCount.Value;
			}
			set
			{
				if (loadedLoadCount.Setter == null)
					loadedLoadCount.Setter = v => ViewState["LoadCount"] = v;
				loadedLoadCount.Value = value;
			}
		}

		private LoadedValue<int?> loadedTitleCapacity;
		/// <summary>
		/// 标题区域所能容纳的字符数（半角字符按每字符1个单位计，全角字符按每字符2个单位计），多出的部分将被截取。
		/// </summary>
		public virtual int? TitleCapacity
		{
			get
			{
				if (loadedTitleCapacity.Loader == null)
					loadedTitleCapacity.Loader = () => (int?)ViewState["TitleCapacity"];
				return loadedTitleCapacity.Value;
			}
			set
			{
				if (loadedTitleCapacity.Setter == null)
					loadedTitleCapacity.Setter = v => ViewState["TitleCapacity"] = v;
				loadedTitleCapacity.Value = value;
			}
		}

		private LoadedValue<string> loadedItemTarget;
		/// <summary>
		/// 列表项的链接目标。
		/// </summary>
		public virtual string ItemTarget
		{
			get
			{
				if (loadedItemTarget.Loader == null)
					loadedItemTarget.Loader = () => (string)ViewState["ItemTarget"];
				return loadedItemTarget.Value;
			}
			set
			{
				if (loadedItemTarget.Setter == null)
					loadedItemTarget.Setter = v => ViewState["ItemTarget"] = v;
				loadedItemTarget.Value = value;
			}
		}

		private LoadedValue<string> loadedItemImageResourceCopyName;
		/// <summary>
		/// 列表项的图片资源副本名。
		/// </summary>
		public virtual string ItemImageResourceCopyName
		{
			get
			{
				if (loadedItemImageResourceCopyName.Loader == null)
					loadedItemImageResourceCopyName.Loader = () => (string)ViewState["ItemImageResourceCopyName"];
				return loadedItemImageResourceCopyName.Value;
			}
			set
			{
				if (loadedItemImageResourceCopyName.Setter == null)
					loadedItemImageResourceCopyName.Setter = v => ViewState["ItemImageResourceCopyName"] = v;
				loadedItemImageResourceCopyName.Value = value;
			}
		}

		private LoadedValue<string> loadedItemDefaultImageUrl;
		/// <summary>
		/// 列表项的默认图片地址。
		/// </summary>
		public virtual string ItemDefaultImageUrl
		{
			get
			{
				if (loadedItemDefaultImageUrl.Loader == null)
					loadedItemDefaultImageUrl.Loader = () => 
						{
							if (ViewState["ItemDefaultImageUrl"]==null)							
								return "/_Client/Controls/ResourcePopupSelector/NotImage.jpg";
							return (string)ViewState["ItemDefaultImageUrl"];
						};
				return loadedItemDefaultImageUrl.Value;
			}
			set
			{
				if (loadedItemDefaultImageUrl.Setter == null)
					loadedItemDefaultImageUrl.Setter = v => ViewState["ItemDefaultImageUrl"] = v;
				loadedItemDefaultImageUrl.Value = value;
			}
		}



		/// <summary>
		/// 标题字段。
		/// </summary>
		public abstract string TitleField { get; }
		/// <summary>
		/// 日期字段。
		/// </summary>
		public virtual string DateTimeField { get { return null; } }
		/// <summary>
		/// 图片资源字段。
		/// </summary>
		public virtual string ImageResourceField { get { return null; } }
		/// <summary>
		/// 获取列表数据。
		/// </summary>
		/// <returns>获取的列表数据。</returns>
		public abstract object GetListData();



		/// <summary>
		/// 列表的目标属性（ target="_blank"）。
		/// </summary>
		public virtual string ListTargetAttribute { get { return string.IsNullOrEmpty(ListTarget) ? "" : @" target=""" + ListTarget + @""""; } }
		/// <summary>
		/// 列表项的目标属性（ target="_blank"）。
		/// </summary>
		public virtual string ItemTargetAttribute { get { return string.IsNullOrEmpty(ItemTarget) ? "" : @" target=""" + ItemTarget + @""""; } }


		/// <summary>
		/// 列表项的原始标题（未做截取等任何操作的原始标题。）。
		/// </summary>
		public virtual string ItemSourceTitle { get { return Eval(TitleField, null).Replace("'", "&apos;").Replace("\"", "&quot;"); } }

		/// <summary>
		/// 列表项的标题。
		/// </summary>
		public virtual string ItemTitle
		{
			get
			{
				return StringExtensions.CutString(ItemSourceTitle, TitleCapacity, "...");
			}
		}

		/// <summary>
		/// 列表项的日期。
		/// </summary>
		public virtual DateTime ItemDateTime
		{
			get
			{
				if (string.IsNullOrEmpty(DateTimeField)) return DateTime.Now;
				return (DateTime)Eval(DateTimeField);
			}
		}

		/// <summary>
		/// 列表项的图片资源（仅为资源标识）。
		/// </summary>
		public virtual string ItemImageResource
		{
			get
			{
				if (string.IsNullOrEmpty(ImageResourceField)) return null;
				return Eval(ImageResourceField, null);
			}
		}

		/// <summary>
		/// 列表项的图片Url。
		/// </summary>
		public virtual string ItemImageUrl
		{
			get
			{
				return GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(ItemImageResource, ItemImageResourceCopyName, null, ItemDefaultImageUrl);
			}
		}

		private LoadedValue<bool> loadedOpenWithDefauleWay;

		public virtual bool OpenWithDefauleWay
		{
			get {
				if (loadedOpenWithDefauleWay.Loader == null)
					loadedOpenWithDefauleWay.Loader = () => BooleanExtensions.ParseOrDefault(Convert.ToString(ViewState["OpenWithDefauleWay"]), true);
				return loadedOpenWithDefauleWay.Value;
			}
			set {
				if (loadedOpenWithDefauleWay.Setter == null)
					loadedOpenWithDefauleWay.Setter = v => ViewState["OpenWithDefauleWay"] = v;
				loadedOpenWithDefauleWay.Value = value;
			}
		}

		/// <summary>
		/// 列表项的图片标签（包含链接地址标签）。
		/// </summary>
		public virtual string ItemImageTag
		{
			get
			{
				string imgUrl = ItemImageUrl;
				if (string.IsNullOrEmpty(imgUrl)) return null;
				
				if (OpenWithDefauleWay)
				{
					return string.Format(@"<a href=""{0}"" target=""_blank""><img src=""{1}"" alt=""{2}"" /></a>"
						, ItemUrl
						, ItemImageUrl
						, ItemSourceTitle);
				}

				return string.Format(@"<a onclick=""window.open('{0}')"" href=""javaScript:void(0);""><img src=""{1}"" alt=""{2}"" /></a>"
						, ItemUrl
						, ItemImageUrl
						, ItemSourceTitle);
			}
		}

		/// <summary>
		/// 列表项的媒体标签，根据不同的资源类型生成不同的标签。
		/// </summary>
		public virtual string ItemMultimediaTag
		{
			get
			{
				string resource = ItemImageResource;
				if (string.IsNullOrEmpty(resource)) return "";

				if (resource.EndsWith(".swf", StringComparison.CurrentCultureIgnoreCase))
				{
					ResourceThumbnailCopyElement copyConfig = ApplicationConfiguration.GetResourceThumbnailCopy(ItemImageResourceCopyName);
					return @"<embed width=""" + copyConfig.Width + @"px"" height=""" + copyConfig.Height + @"px"" name=""plugin"" src=""" + GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(resource, null) + @""" type=""application/x-shockwave-flash"" />";
				}
				return ItemImageTag;
			}
		}

		/// <summary>
		/// 列表项的Url地址。
		/// </summary>
		public abstract string ItemUrl { get; }
	}
}